package avicit.bdp.dgs.qa.service;

import avicit.bdp.common.service.dto.DataSourceDTO;
import avicit.bdp.common.service.service.DataSourceService;
import avicit.bdp.common.database.ColumnMeta;
import avicit.bdp.common.database.TableMeta;
import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @金航数码科技有限责任公司
 * @作者：developer
 * @邮箱：developer@avic-digital.com
 * @创建时间： 2023-04-27
 * @类说明：BdpDgsDatasourceService
 * @修改记录：
 * @注意事项：
 * @主要功能：数据质量数据源Service
 */
@Service
public class BdpDgsDatasourceService {
    private static final Logger logger = LoggerFactory.getLogger(BdpDgsDatasourceService.class);

    @Autowired
    private DataSourceService dataSourceService;

    /**
     * 根据数据源ID查询dbName
     *
     * @param datasourceId 数据源ID
     * @return
     */
    public List<String> getDbName(String datasourceId) {
        if (StringUtils.isEmpty(datasourceId)) {
            logger.warn("数据源ID为空，获取数据库名称失败.");
            return new ArrayList<>();
        }

        DataSourceDTO dto = this.dataSourceService.queryDataSourceById(datasourceId);
        if (dto == null) {
            logger.warn("未找到数据源，dsId" + datasourceId);
            return new ArrayList<>();
        }
        if (StringUtils.isEmpty(dto.getConnectionParams())) {
            logger.warn("数据源连接参数为空，dsId" + datasourceId);
            return new ArrayList<>();
        }
        JSONObject connObj = JSONObject.parseObject(dto.getConnectionParams());
        String dbName = connObj.getString("database");
        if (StringUtils.isEmpty(dbName)) {
            logger.warn("数据库名称为空，dsId" + datasourceId);
            return new ArrayList<>();
        }

        List<String> dbList = new ArrayList<>();
        dbList.add(dbName);
        return dbList;
    }

    /**
     * 根据数据源ID查询表
     *
     * @param datasourceId 数据源ID
     * @return
     */
    public List<TableMeta> getTableList(String datasourceId) {
        List<TableMeta> result = new ArrayList<>();
        if (StringUtils.isEmpty(datasourceId)) {
            logger.warn("数据源ID为空，获取表失败.");
            return new ArrayList<>();
        }

        try {
            result = this.dataSourceService.getTableListByDatasourceId(datasourceId, false);
        } catch (Exception e) {
            logger.error("查询表失败,errMsg=" + e.getMessage());
        }

        return result;
    }

    /**
     * 根据数据源ID和表名称查询字段信息
     *
     * @param datasourceId 数据源ID
     * @param tableName    表名称
     * @return
     */

    public List<ColumnMeta> getColumnList(String datasourceId, String tableName) {
        List<ColumnMeta> result = new ArrayList<>();
        if (StringUtils.isEmpty(datasourceId)) {
            logger.error("数据源ID为空，获取表失败.");
            return new ArrayList<>();
        }

        result = this.dataSourceService.getColumnList(datasourceId, tableName);
        if (CollectionUtils.isEmpty(result)) {
            logger.warn(String.format("字段信息为空,dsId=%s,tableName=%s.", datasourceId, tableName));
        }

        return result;
    }
}
